<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/jsp/taglib.jsp" %>

<!DOCTYPE html>
<html>
<head>
    <title>资料发布</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入bootstrap -->
    <link rel="stylesheet" type="text/css" href="${ctx}/css/bootstrap.min.css">
    <!-- 引入JQuery  bootstrap.js-->
    <script src="${ctx}/js/jquery-3.2.1.min.js"></script>
    <script src="${ctx}/js/ajaxfileupload.js"></script>
    <script src="${ctx}/js/bootstrap.min.js"></script>
    <script src="${ctx}/js/layer/layer.js"></script>

</head>
<body>
<!-- 顶栏 -->
<jsp:include page="top.jsp"/>
<!-- 中间主体 -->
<div class="container" id="content">
    <div class="row">
        <jsp:include page="menu.jsp"/>
        <div class="col-md-10">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <div class="row">
                        <h1 class="col-md-5">资料发布</h1>
                        <%--                        <form class="bs-example bs-example-form col-md-5" role="form" style="margin: 20px 0 10px 0;"--%>
                        <%--                              action="${ctx}/teacher/findCourse" id="form1" method="post">--%>
                        <%--                            <div class="input-group">--%>
                        <%--                                <input type="text" class="form-control" placeholder="请输入课程名" name="name">--%>
                        <%--                                <span class="input-group-addon btn" onclick="document.getElementById('form1').submit"--%>
                        <%--                                      id="sub">搜索</span>--%>
                        <%--                            </div>--%>
                        <%--                        </form>--%>
                        <button type="button" style="margin-top: 20px" class="btn btn-primary" data-toggle="modal"
                                data-target="#exampleModal"
                        >课程资料添加
                        </button>

                    </div>
                </div>
                <table class="table table-bordered">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>实验名称</th>
                        <th>大课PPT</th>
                        <th>实验文档</th>
                        <th>资料</th>
                        <th>发布时间</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <c:forEach items="${ResList}" var="item">
                        <tr>
                            <td>${item.id}</td>
                            <td>${item.courseLibName}</td>
                            <td>${item.pptName}</td>
                            <td>${item.libName}</td>
                            <td>${item.resName}</td>
                            <td>${item.gmtCreate}</td>
                            <td>
                                <button class="btn btn-default btn-xs btn-danger"
                                        onClick="isDelete(${item.id})">删除
                                </button>
                            </td>
                        </tr>
                    </c:forEach>
                    </tbody>
                </table>
                <div class="panel-footer">
                    <c:if test="${pagingVO != null}">
                        <nav style="text-align: center">
                            <ul class="pagination">
                                <li><a href="${ctx}/student/showCourse?page=${pagingVO.upPageNo}">&laquo;上一页</a></li>
                                <li class="active"><a href="">${pagingVO.curentPageNo}</a></li>
                                <c:if test="${pagingVO.curentPageNo+1 <= pagingVO.totalCount}">
                                    <li>
                                        <a href="${ctx}/student/showCourse?page=${pagingVO.curentPageNo+1}">${pagingVO.curentPageNo+1}</a>
                                    </li>
                                </c:if>
                                <c:if test="${pagingVO.curentPageNo+2 <= pagingVO.totalCount}">
                                    <li>
                                        <a href="${ctx}/student/showCourse?page=${pagingVO.curentPageNo+2}">${pagingVO.curentPageNo+2}</a>
                                    </li>
                                </c:if>
                                <c:if test="${pagingVO.curentPageNo+3 <= pagingVO.totalCount}">
                                    <li>
                                        <a href="${ctx}/student/showCourse?page=${pagingVO.curentPageNo+3}">${pagingVO.curentPageNo+3}</a>
                                    </li>
                                </c:if>
                                <c:if test="${pagingVO.curentPageNo+4 <= pagingVO.totalCount}">
                                    <li>
                                        <a href="${ctx}/student/showCourse?page=${pagingVO.curentPageNo+4}">${pagingVO.curentPageNo+4}</a>
                                    </li>
                                </c:if>
                                <li><a href="${ctx}/student/showCourse?page=${pagingVO.totalCount}">最后一页&raquo;</a></li>
                            </ul>
                        </nav>
                    </c:if>
                </div>
            </div>

        </div>
    </div>
</div>
<div class="container" id="footer">
    <div class="row">
        <div class="col-md-12"></div>
    </div>
</div>
<%--模态框 开始--%>
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="exampleModalLabel">发布实验</h4>
            </div>
            <div class="modal-body">
                <form id="totalForm" method="post" action="${ctx}/teacher/addRes">
                    <div class="form-group">
                        <label for="recipient-name" class="control-label">实验名称:</label>
                        <input type="text" name="courseLibName" class="form-control" id="recipient-name">
                    </div>
                    <div class="form-group">
                        <input class="pptName" type="hidden" name="pptName" value="">
                        <input class="pptUrl" type="hidden" name="pptUrl" value="">
                        <input class="libName" type="hidden" name="libName" value="">
                        <input class="libUrl" type="hidden" name="libUrl" value="">
                        <input class="resName" type="hidden" name="resName" value="">
                        <input class="resUrl" type="hidden" name="resUrl" value="">
                        <input class="gmtCreate" type="hidden" name="gmtCreate" value="">
                    </div>
                </form>
                <form id="fileUpload1" enctype="multipart/form-data">
                    <label for="file1">大课PPT:</label>
                    <input name="file" type="file" id="file1" onchange="uploadFile(this,1)">
                </form>
                <br/>
                <form id="fileUpload2" enctype="multipart/form-data">
                    <label for="file2">实验文档:</label>
                    <input name="file" type="file" id="file2" onchange="uploadFile(this,2)">
                </form>
                <br/>
                <form id="fileUpload3" enctype="multipart/form-data">
                    <label for="file3">资料:</label>
                    <input name="file" type="file" id="file3" onchange="uploadFile(this,3)">
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="sub()">发布</button>
            </div>
        </div>
    </div>
</div>
<%--模态框 结束--%>
</body>
<script type="text/javascript">
    <%--设置菜单中--%>
    $("#nav li:nth-child(2)").addClass("active")
    <c:if test="${pagingVO != null}">
    if (${pagingVO.curentPageNo} == ${pagingVO.totalCount}) {
        $(".pagination li:last-child").addClass("disabled")
    }
    ;

    if (${pagingVO.curentPageNo} == ${2}) {
        $(".pagination li:nth-child(2)").addClass("disabled")
    }
    ;
    </c:if>

    // $("#sub").click(function () {
    //     $("#form1").submit();
    // });

    /*模态框 开始*/
    $('#exampleModal').on('show.bs.modal', function (event) {
        var button = $(event.relatedTarget) // Button that triggered the modal
        var recipient = button.data('whatever') // Extract info from data-* attributes
        var modal = $(this)
        modal.find('.modal-title').text('资料发布')
        modal.find('.modal-body input').val(recipient)
    })
    /*模态框 结束*/

    // 文件上传
    function uploadFile(obj, type) {
        var formData = new FormData($('#fileUpload' + type)[0]);
        $.ajax({
            type: 'post',
            url: "http://localhost:8222/eduoss/fileoss",
            data: formData,
            cache: false,
            processData: false,
            contentType: false,
            success: function (data) {
                console.log(data)
                switch (type) {
                    case 1: {
                        $('.pptName').val(data.data.fileName)
                        $('.pptUrl').val(data.data.url)
                        break;
                    }
                    case 2: {
                        $('.libName').val(data.data.fileName)
                        $('.libUrl').val(data.data.url)
                        break;
                    }
                    case 3: {
                        $('.resName').val(data.data.fileName)
                        $('.resUrl').val(data.data.url)
                        break;
                    }
                }
            },
            error: function (data) {
                console.log(data)
            }
        });
    }

    // 提交发布表单
    function sub() {
        let date = dateFormat("YYYY-mm-dd HH:MM", new Date())
        $('.gmtCreate').val(date)
        $("#totalForm").submit();
    }

    // 格式化时间
    function dateFormat(fmt, date) {
        let ret;
        const opt = {
            "Y+": date.getFullYear().toString(),        // 年
            "m+": (date.getMonth() + 1).toString(),     // 月
            "d+": date.getDate().toString(),            // 日
            "H+": date.getHours().toString(),           // 时
            "M+": date.getMinutes().toString(),         // 分
            "S+": date.getSeconds().toString()          // 秒
            // 有其他格式化字符需求可以继续添加，必须转化成字符串
        };
        for (let k in opt) {
            ret = new RegExp("(" + k + ")").exec(fmt);
            if (ret) {
                fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
            }
            ;
        }
        ;
        return fmt;
    }

    // 删除资料条目
    function isDelete(id) {
        //询问框
        layer.confirm('您确定删除吗？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            location.href = '${ctx}/teacher/deleteInfoById?id=' + id
        }, function () {
        });
    }


</script>
</html>